package org.loofer.ext.component.injector;

import android.content.Context;
import android.text.TextUtils;
import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.FileUtils;
import org.loofer.ext.utils.SecurityUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public final class ClassLoaderInjector {
    private static final int BUFFER_SIZE = 128;
    private static final String CHECKSUM_SUFFIX = ".checksum";
    private static final String LIBS_DIR = "libs";
    private static final String LIB_SUFFIX = ".zip";
    private static final String TAG = "ClassLoaderInjector";

    private ClassLoaderInjector() {
    }

    public static void injectWithAssets(Context context, String assetPath) throws Exception {
        if (TextUtils.isEmpty(assetPath)) {
            throw new RuntimeException("invalid asset path");
        }
        String targetPath = generateTargetPath(context, assetPath);
        if (TextUtils.isEmpty(targetPath)) {
            throw new RuntimeException("invalid target path for asset " + assetPath);
        }
        File targetFile = new File(targetPath);
        if (isFileValid(targetFile) && !verify(context, assetPath, targetPath)) {
            Logger.w(TAG, targetPath + " not pass the verification for " + assetPath);
            FileUtils.delete(targetFile);
        }
        FileUtils.copyAssets(context, assetPath, targetPath);
        if (isFileValid(targetFile)) {
            SystemClassLoaderInjector.inject(context, targetPath);
            context.getClassLoader().loadClass(SystemClassLoaderInjector.ANTI_LAZY_LOAD);
            Logger.i(TAG, "succeed to perform inject for " + assetPath);
            return;
        }
        throw new FileNotFoundException("fail to copy asset file " + assetPath + " to target " + targetPath);
    }

    private static boolean verify(Context context, String assetPath, String targetPath) {
        String targetChecksum = SecurityUtils.digest(new File(targetPath));
        if (TextUtils.isEmpty(targetChecksum)) {
            Logger.w(TAG, "fail to read target checksum for " + targetPath);
            return false;
        }
        String checksum = readStringFromAssets(context, assetPath + CHECKSUM_SUFFIX);
        if (!TextUtils.isEmpty(checksum)) {
            return checksum.equalsIgnoreCase(targetChecksum);
        }
        Logger.w(TAG, "fail to read checksum for " + assetPath);
        return false;
    }

    private static String readStringFromAssets(Context context, String path) {
        Throwable e;
        Throwable th;
        String s = null;
        Reader reader = null;
        try {
            Reader reader2 = new InputStreamReader(context.getAssets().open(path));
            try {
                char[] buffer = new char[128];
                StringBuilder sb = new StringBuilder();
                while (true) {
                    int count = reader2.read(buffer);
                    if (count <= 0) {
                        break;
                    }
                    sb.append(buffer, 0, count);
                }
                s = sb.toString();
                if (reader2 != null) {
                    try {
                        reader2.close();
                        reader = reader2;
                    } catch (IOException e2) {
                        reader = reader2;
                    }
                }
            } catch (IOException e3) {
                e = e3;
                reader = reader2;
                try {
                    Logger.d(TAG, "error occurs while reading assets " + path, e);
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e4) {
                        }
                    }
                    return s;
                } catch (Throwable th2) {
                    th = th2;
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e5) {
                        }
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                reader = reader2;
                if (reader != null) {
                    reader.close();
                }
                throw th;
            }
        } catch (IOException e6) {
            e = e6;
            Logger.d(TAG, "error occurs while reading assets " + path, e);
            if (reader != null) {
                reader.close();
            }
            return s;
        }
        return s;
    }

    private static String generateTargetPath(Context context, String name) {
        return context.getDir(LIBS_DIR, 0).getAbsolutePath() + File.separator + (SecurityUtils.digest(name) + LIB_SUFFIX);
    }

    private static boolean isFileValid(File file) {
        return file != null && file.exists() && file.isFile() && file.length() > 0;
    }
}
